<?php
/**
 * SiteData.class.php
 *
 * @author fx
 * @date   18-1-15 下午1:17
 */

namespace Center;

use Common\ModuleBase;
use Common\Bridge;

class SiteData extends ModuleBase
{
    protected static $_conf = [
        'db' => [
            'w' => Bridge::DB_CENTER_W,
            'r' => Bridge::DB_CENTER_R
        ],
        'table' => 'SiteData',
        'fields' => [
            'Id' => self::VAR_TYPE_KEY,
            'Name' => self::VAR_TYPE_STRING,
            'Content' => self::VAR_TYPE_STRING,
            'UpdateTime' => self::VAR_TYPE_STRING,
            'CreateTime' => self::VAR_TYPE_STRING,
        ],
        'mc' => Bridge::MEMCACHE_DEFAULT,
        // 可选, MC KEY, "KeyName:%d", %d会用主键ID替代
        'mckey' => 'SiteData:%d',
        // 可选, 超时时间, 默认不过期
        'mctime' => 86400,
    ];

    const MC_SITEDATA_KEY = 'SiteData:';

    /**
     * 通过KEY获取数据
     *
     * @param     $uid
     * @param int $status
     * @param int $valid
     * @param int $shopid
     * @param int $offset
     * @param int $limit
     * @return array
     */
    public static function getDataByKey($key)
    {
        $where = [
            'Name' => $key
        ];
        $extra = [
            'type' => parent::MOD_TYPE_DB,
            'fields' => 'Content',
            'get_count' => 0,
            'mctime' => 86400
        ];
        $data = parent::getDataByFields($where, $extra);
        return $data['data'][0]['Content'];
    }

    public static function updateSiteData($key, $content)
    {
        $where = [
            'Name' => $key
        ];
        $extra = [
            'type' => parent::MOD_TYPE_DB,
            'fields' => 'Id, Content',
            'mctime' => 86400
        ];
        $data = parent::getDataByFields($where, $extra);
        if ($data['data'][0]['Id'] > 0) {
            $rs = self::update($data['data'][0]['Id'], ['Content' => $content]);
            return $rs;
        } else {
            return false;
        }
    }

    /**
     * @see ModuleBase::add() 新增
     * @see ModuleBase::update() 更新
     * @see ModuleBase::getInfoByIds() 按id查询
     * @see ModuleBase::getList() 条件查询
     * @see ModuleBase::delete() 删除
     */

}